//This code was created with the help of Cairngorm Creator by Tyler Beck.
//The portions of this document created by Cairngorm Creator 
//are provided on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
//CONDITIONS OF ANY KIND, either express or implied.
//========================================================



package net.optix.ip.commands
{
	import com.adobe.cairngorm.business.Responder;
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import net.optix.ip.business.*;
	import net.optix.ip.events.GetInventionsEvent;
	import net.optix.ip.events.GetUserEvent;
	import net.optix.ip.events.UserLoginEvent;
	import net.optix.ip.model.ModelLocator;
	import views.ViewInstance;

	public class UserLoginCommand implements Command, Responder
	{
		// attributes ============================
		private var model : ModelLocator = ModelLocator.getInstance();


		// functions ============================
		public function execute( event:CairngormEvent ) : void
		{
			
			var myEvent:UserLoginEvent = UserLoginEvent( event );
			
			var delegate:UserDelegate = new UserDelegate();     
			delegate.userLogin( this,myEvent.vo );
			model.result	=	"Loging ...";
		}
		
		//----------------------------------------------------------------------------
		public function onResult( event : * = null ) : void
		{		
			//ViewInstance.showMessage("",event.result);
			trace(event.result);
			var userID:int	=	0;
			
			try
			{
				var result:int		=	event.result.role;
				userID				=	event.result.UserID;
				switch(result)
				{
					case -1:
						model.result	=	"The password is not correct!";
						break;
					case 0:
						model.result	=	"The user is not exist!";
						model.close		=	"no";
						model.close		=	"yes";
						
						break;
					case 1:
						model.result	=	"Successfully logged in , you are in the client screen!";
						model.role		=	1;						
						model.close		=	"no";
						model.close		=	"yes";
						break;
					case 2:
						model.result	=	"Successfully logged in , you are in the inventor screen!";
						
						model.role		=	2;
						model.close		=	"no";
						model.close		=	"yes";
						
						break;
					case 3:
						model.result	=	"Successfully logged in , you are in the firm screen!";
						model.close		=	"yes";
						model.role		=	3;	
						
						break;
					case 4:
						model.result	=	"Successfully logged in , you are in the practitioner screen!";
						model.close		=	"yes";
						model.role		=	4;
						
						break;	
					case 5:
						model.result	=	"Successfully logged in , you are in the manager screen!";
						model.close		=	"yes";
						model.role		=	5;
						
						model.view		=	0;
						model.view		=	ViewInstance.ViewClients;
						break;	
				}
			}
			catch(error:Error)
			{
				model.role			=	0;
				model.result	=	"Program error";
				
			}
			model.status	=	model.result;
			model.status	=	"";
			model.bSubmit	=	true;
			
			if(model.role > 0 && model.role!=5)
			{
				var event1:GetUserEvent	=	new GetUserEvent(userID);
				CairngormEventDispatcher.getInstance().dispatchEvent(event1);
			}
		}
		
		//----------------------------------------------------------------------------
		public function onFault( event : * = null ) : void
		{
			//ViewInstance.showMessage("",event.fault);
			model.result	=	"Program error !";
			model.bSubmit	=	true;
		}

	}
}